package com.doge.walknovel.core.account

import com.facebook.AccessToken
import org.json.JSONObject

data class FacebookUser(
    val nickname: String?,
    val avatarUrl: String?,
    val link: String?,
    val id: String?,
    val email: String?,
    val gender: Int?,
    val accessToken: String?
) {
    companion object {
        fun from(data: JSONObject?, token: AccessToken?): FacebookUser? {
            if (data == null) return null
            if (token == null) return null
            return FacebookUser(
                nickname = data.optString("name"),
                link = data.optString("link"),
                avatarUrl = data.optJSONObject("picture")?.optJSONObject("data")?.optString("url"),
                id = data.optString("id"),
                email = data.optString("email"),
                gender = when (data.optString("gender")) {
                    "male" -> 1
                    "famale" -> 0
                    else -> null
                },
                accessToken = token.token
            )
        }
    }
}